package com.laikan.reader.database;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.QueryBuilder;
import thirdparty.greendao.database.BookDao;
import thirdparty.greendao.database.DaoSession;

import java.util.List;

/**
 * Created by anrun on 8/10/16.
 */
public class DBBook {

    private DaoSession daoSession;
    private BookDao bookDao;
    private static DBBook dbBook;

    private DBBook() {
        daoSession = DBManager.getInstance().getDaoSession();
        bookDao = daoSession.getBookDao();
    }

    public static DBBook getInstance() {
        if (dbBook == null) {
            synchronized (DBBook.class) {
                if (dbBook == null) {
                    dbBook = new DBBook();
                }
            }
        }
        return dbBook;
    }

    public long getCount() {
        return bookDao.count();
    }

    public void insertBook(Book book) {
        // 插入前判断该book是否已在数据库中
        if (queryBookById(book.getId()) != null) {
            //已经加入书架的情况

            if (book.getSortIndex() == 0) {
                book.setSortIndex(queryBookById(book.getId()).getSortIndex());
            }else if (!getLastBookId().equals(book.getId())) {
                book.setSortIndex(getLastBookSortIndex() + 65535);

            }
            update(book);
        }else {//还没有加入书架的情况，将加入书架
            book.setSortIndex(getLastBookSortIndex() + 65535);


            bookDao.insert(book);
        }
    }

    public void insetBookList(List<Book> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        bookDao.insertInTx(list);
    }

    public Book queryBookById(String bookId) {
        QueryBuilder<Book> qb = bookDao.queryBuilder();
        qb.where(BookDao.Properties.Id.eq(bookId));
        if (qb.list() == null || qb.list().isEmpty()) {
            return null;
        }
        return qb.list().get(0);
    }

    public List<Book> queryAllBook() {
        QueryBuilder<Book> qb = bookDao.queryBuilder();

        qb.orderDesc(BookDao.Properties.SortIndex);
        return qb.list();
    }

    public void deleteByBookId(String bookId) {
        QueryBuilder<Book> qb = bookDao.queryBuilder();
        DeleteQuery<Book> dq = qb.where(BookDao.Properties.Id.eq(bookId)).buildDelete();
        dq.executeDeleteWithoutDetachingEntities();
    }

    public void clear() {
        bookDao.deleteAll();
    }

    public void update(Book book) {
        QueryBuilder<Book> qb = bookDao.queryBuilder();
        qb.where(BookDao.Properties.Id.eq(book.getId()));
        List<Book> list = qb.list();
        if (list == null || list.isEmpty()) {
            return;
        }else {
            bookDao.insertOrReplace(book);
        }
    }

    public double getLastBookSortIndex() {
        List<Book> books = queryAllBook();
        if (books.size() > 0) {

            return books.get(0).getSortIndex();
        }else {
            return 0;
        }
    }

    public String getLastBookId() {
        List<Book> books = queryAllBook();
        if (books.size() != 0) {

            return books.get(0).getId();
        }else {
            return "";
        }
    }
}
